﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Launcher
{
    public class PathMap
    {
        private static string[] symbols = new string[] { "~\\\\", "@App\\\\", "#ProgramData\\\\", "#GAC\\\\", "#SYSTEM32\\\\" };

        /// <summary>
        /// 获取文件的路径，将路径中的映射关系转换为绝对路径
        /// </summary>
        /// <param name="appInfo"></param>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        public static string GetTargetFilePath(Common.AppInfo appInfo, string targetPath)
        {
            for (int index = 0; index < symbols.Length; index++)
            {
                if (targetPath.StartsWith(symbols[index]))
                {
                    targetPath = targetPath.Replace(symbols[index], GetFoldName(appInfo, symbols[index]));
                }
            }
            return targetPath;
        }

        /// <summary>
        /// 获取更新文件在已下载更新包中的路径
        /// </summary>
        /// <param name="appInfo">应用程序信息</param>
        /// <param name="filePath">文件路径</param>
        /// <param name="downlodFolderName">已下载更新包名称</param>
        /// <returns></returns>
        public static string GetFilePath(Common.AppInfo appInfo, string filePath, string downlodFolderName)
        {
            string oFilePath = appInfo.Directory + "Patches\\" +appInfo.Product+"\\"+appInfo.Type+"\\" + downlodFolderName + "\\Files\\";
            if (filePath.StartsWith(".\\"))
            {
                filePath = oFilePath + filePath.Replace(".\\", "");
            }
            else
            {
                filePath = oFilePath + filePath;
            }
            return filePath;
        }

        /// <summary>
        /// 获取解压以后要更新的文件的路径
        /// </summary>
        /// <param name="appInfo"></param>
        /// <returns></returns>
        public static string GetUnZipzFilePath(Common.AppInfo appInfo,string filePath,string downlodFolderName)
        {

            string oFilePath = appInfo.Directory + "Patches\\" + appInfo.Product + "\\" + appInfo.Type + "\\" + downlodFolderName + "\\Files\\";
            if (filePath.StartsWith(".\\"))
            {
                filePath = oFilePath + filePath.Replace(".\\", "");
            }
            else
            {
                filePath = oFilePath + filePath;
            }
            return filePath;
        }

        /// <summary>
        /// 获取将要下载更新包中文件的下载路径
        /// </summary>
        /// <param name="directory">应用程序的安装路径</param>
        /// <param name="downlodFolderName">更新文件要下载到的文件名</param>
        /// <param name="sign">符号"\\"是否为子文件夹</param>
        /// <returns></returns>
        public static string GetDownloadFilePath(string directory, string downlodFolderName, string sign)
        {
            if (string.IsNullOrEmpty(sign))
            {
                return directory + downlodFolderName;
            }
            return directory + sign + downlodFolderName;
        }

        /// <summary>
        /// 下载更新包时，需要创建的文件夹
        /// </summary>
        /// <param name="path">将要创建文件夹的路径</param>
        /// <param name="createFolderName">创建文件夹的名称</param>
        public static void CreateSubdirectory(string path, string createFolderName)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            directoryInfo.CreateSubdirectory(createFolderName);
        }

        /// <summary>
        /// 下载更新包时，需要创建的文件夹
        /// </summary>
        /// <param name="path">将要创建文件夹的路径</param>
        /// <param name="scheduleMode">版本计划更新的模式</param>
        /// <param name="fromVersion">起始版本号</param>
        /// <param name="targetVersion">目标版本号</param>
        public static string CreateSubdirectory(string path, string scheduleMode, string fromVersion, string targetVersion)
        {
            if (string.IsNullOrEmpty(scheduleMode))
            {
                return null;
            }
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            string folderName = string.Empty;
            if (scheduleMode == "Full")
            {
                folderName = scheduleMode;
            }
            else
            {
                folderName = fromVersion;
            }
            directoryInfo.CreateSubdirectory(string.Format("Patch_{0}-{1}", folderName, targetVersion));
            return string.Format("Patch_{0}-{1}", folderName, targetVersion);
        }


        public static string SubFilePath(string filePath,string fileName,string destFullName,string destRootFullName,ref string _xmlDownlodFileName)
        {
            if (filePath.StartsWith("."))
            {
                int startIndex = filePath.IndexOf("\\");
                int lastIndex = filePath.LastIndexOf("\\\\");
                string folderName = filePath.Substring(3, lastIndex - startIndex - 2);
                DirectoryInfo info = new DirectoryInfo(destFullName);
                info.CreateSubdirectory(folderName);
                return destFullName += "\\" + folderName + "\\" + fileName;
            }
            else if (filePath.StartsWith("~"))
            {
                _xmlDownlodFileName = "";
                _xmlDownlodFileName = fileName;
                return destRootFullName + "\\" + fileName;
            }
            else
            {
               return destFullName += "\\" + fileName;
            }
            return null;
        }

        /// <summary>
        /// ~/:
        ///  更新程序启动目录
        /// @App/:
        ///  被更新的应用程序的目录
        /// #ProgramData/
        ///  系统的ProgramdData目录
        /// #GAC/:
        ///  系统的gac目录
        /// #SYSTEM32/:
        ///  系统的Windows目录
        /// </summary>
        /// <param name="appInfo"></param>
        /// <param name="symbol"></param>
        /// <returns></returns>
        public static string GetFoldName(Common.AppInfo appInfo, string symbol)
        {
            string foldPath = string.Empty;
            switch (symbol)
            {
                //程序启动目录
                case "~\\\\":
                    foldPath = AppDomain.CurrentDomain.BaseDirectory;
                    break;
                //当前方案文件所在的目录
                case "@App\\\\":
                    foldPath = appInfo.Directory;
                    break;
                //系统所使用的ProgramData目录
                case "#ProgramData\\\\":
                    foldPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                    break;
                //gac目录
                case "#GAC\\\\":
                    foldPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\assembly\";
                    break;
                //系统目录Windows目录
                case "#SYSTEM32\\\\":
                    foldPath = System.Environment.GetFolderPath(Environment.SpecialFolder.System);
                    break;
            }
            if (!string.IsNullOrEmpty(foldPath) && !foldPath.EndsWith(@"\"))
            {
                foldPath = foldPath + @"\";
            }
            return foldPath;
        }
    }
}
